package myDaos;

import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import model.entities.SalesMatchingDayInfo;
import model.entities.SalesMatchingInfo;
import model.entities.SalesMatchingInvoiceInfo;
import model.entities.SalesMatchingLineidInfo;


@Stateless
public class MatchingReportDao {

	@PersistenceContext(unitName = "UnileverProcedure")
    private EntityManager em;
	
	public EntityManager getEntityManager() {
        return em;
    }
	
	public Boolean callMatchingReportProcedure(String p_country,String p_distributor_no,Date p_from_date,Date p_to_date){
		Boolean isCalled;
		try {
		    Query q = getEntityManager().createNativeQuery("{call FINAL_MATCHING_REPORT_TEST(:country, :distributor, :from_date, :to_date)}").setParameter("country",p_country).setParameter("distributor",p_distributor_no).setParameter("from_date",p_from_date).setParameter("to_date",p_to_date);
		    q.executeUpdate();
		    isCalled=true;
	} catch (Exception e) {
		e.printStackTrace();
		isCalled=false;
	}
		return isCalled;
	}
	
	public List<SalesMatchingDayInfo> getMatchingReportListDayLevel(String color){
		List<SalesMatchingDayInfo> mrlist=null;
		try {
			StringBuffer queryBiffer = new StringBuffer();
			queryBiffer.append(" SELECT s FROM SalesMatchingDayInfo s where 1=1"); 
			
			if(color!=null)
    	    {
				queryBiffer.append("  AND s.color not like :color") ;	
    	    }

			Query q = getEntityManager().createQuery(queryBiffer.toString());
			if(color!=null){q.setParameter("color", color);}
			mrlist = q.getResultList();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mrlist;
	}
	
	public List<SalesMatchingInvoiceInfo> getMatchingReportListInvoiceLevel(String color){
		List<SalesMatchingInvoiceInfo> mrlist=null;
		try {
			StringBuffer queryBiffer = new StringBuffer();
			queryBiffer.append(" SELECT s FROM SalesMatchingInvoiceInfo s where 1=1"); 

			if(color!=null)
    	    {
				queryBiffer.append("  AND s.color not like :color") ;	
    	    }
			Query q = getEntityManager().createQuery(queryBiffer.toString());
			if(color!=null){q.setParameter("color", color);}
			mrlist = q.getResultList();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mrlist;
	}
	
	public List<SalesMatchingLineidInfo> getMatchingReportListLineidLevel(String color){
		List<SalesMatchingLineidInfo> mrlist=null;
		try {
			StringBuffer queryBiffer = new StringBuffer();
			queryBiffer.append(" SELECT s FROM SalesMatchingLineidInfo s where 1=1"); 
			if(color!=null)
    	    {
				queryBiffer.append("  AND s.color not like :color") ;	
    	    }
			Query q = getEntityManager().createQuery(queryBiffer.toString());
			if(color!=null){q.setParameter("color", color);}
			mrlist = q.getResultList();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mrlist;
	}
	
}
